﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */


using System;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using PsTFS.Objects;
using PsTFS.Provider;
using System.IO;
using PsTFS.Objects.Methodes;


namespace PsTFS.Commandes.Add
{
    /// <summary>
    /// Ajout d'un ou plusieurs fichiers
    /// </summary>
    /// <example>
    /// ========================================================
    /// Windows PowerShell
    /// PowerShell for Team System
    /// By Noham Choulant http://choulant.blogspot.com
    /// ========================================================
    /// 
    /// PsTFS > : $tfs = get-tfs http://colmar:8080
    /// 
    /// PsTFS > : add-file -tfs $tfs -NameProject Test -source C:\Users\Administrateur\Desktop\PowerShell\ExemplePsTFS.txt -description "Ajout de fichier"
    /// 
    /// Pending add on c:\temp\MergeTFSPsTFS\daa6edbf-7456-4b65-a5d4-a564d66a91a8\TestExemplePsTFS.txt
    /// 
    /// Your current pending changes:
    ///   path: c:\temp\MergeTFSPsTFS\daa6edbf-7456-4b65-a5d4-a564d66a91a8\TestExemplePsTFS.txt, change: add
    /// 
    /// Checking in c:\temp\MergeTFSPsTFS\daa6edbf-7456-4b65-a5d4-a564d66a91a8\TestExemplePsTFS.txt
    /// Checked in changeset 16
    /// 
    /// PsTFS > :
    /// 
    /// </example>
    [Cmdlet(VerbsCommon.Add, "Files")]
    public class Add_Files : PsCmdlet
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public Add_Files()
        {
            if (TFSProvider.s_root != null)
            {
                this.Destination = TFSProvider.s_root;
            }
            this.Description = string.Empty;
            this.Recursion = RecursionType.None;

        }
        /// <summary>
        /// Lancement du processus d'ajout des fichiers
        /// </summary>
        protected override void ProcessRecord()
        {
            if (string.IsNullOrEmpty(Destination))
            {
                WriteError(new ErrorRecord(new ArgumentException("Not target!!!"), "PsTFS", ErrorCategory.ObjectNotFound, Destination));
            }
            try
            {
                //Création du répertoire temporaire pour le workspace
                string tmp = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\tempPsTFS\" + Guid.NewGuid().ToString();

                int ret = Methode.AddFiles(tmp, Destination, Source, Description, Recursion);

                Console.WriteLine("Successfully added files");

                Directory.Delete(tmp);

                WriteObject(ret);
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
                WriteObject(0);
            }
            finally
            {
                base.ProcessRecord();
            }

        }


        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Add-Files is end");
            base.EndProcessing();
        }
        
        /// <summary>
        /// Object contenant Team Foundation Server 2008
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS
        {
            set { Context.GetContext().Tfs = value; }
        }

        /// <summary>
        /// Source des fichiers
        /// </summary>
        [Parameter(Position = 1, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Source { get; set; }

        /// <summary>
        /// Destination
        /// </summary>
        [ValidateNotNullOrEmpty]
        [Parameter(Position = 2)]
        public string Destination { get; set; }

        /// <summary>
        /// Description
        /// </summary>
        [ValidateNotNullOrEmpty]
        [Parameter(Position = 3)]
        public string Description { get; set; }

        /// <summary>
        /// Type de récurtion
        /// </summary>
        [ValidateNotNullOrEmpty]
        [Parameter(Position = 4)]
        public RecursionType Recursion { get; set; }

        /// <summary>
        /// Affiche le détail
        /// </summary>
        [Parameter(Position = 5)]
        [ValidateNotNullOrEmpty]
        public bool Detail { get; set; }
    }
}


